package top.zsyle.product.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import top.zsyle.basic.jwt.UserInfo;
import top.zsyle.basic.util.CodeGenerateUtils;
import top.zsyle.basic.util.LoginContext;
import top.zsyle.order.domain.OrderProduct;
import top.zsyle.pet.dto.AcceptDto;
import top.zsyle.product.domain.Product;
import top.zsyle.product.domain.ProductMsg;
import top.zsyle.order.mapper.OrderProductMapper;
import top.zsyle.product.mapper.ProductMapper;
import top.zsyle.product.mapper.ProductMsgMapper;
import top.zsyle.product.service.IProductMsgService;
import top.zsyle.basic.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import top.zsyle.user.domain.User;
import top.zsyle.user.mapper.UserMapper;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsy
 * @since 2022-12-09
 */
@Service
public class ProductMsgServiceImpl extends BaseServiceImpl<ProductMsg> implements IProductMsgService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderProductMapper orderMapper;

    @Autowired
    private ProductMsgMapper productMsgMapper;

    /**
     * 点击购买业务，生成购买信息单
     * @param productId
     */
    @Override
    public void publish(Long productId) {
        Product product = productMapper.findById(productId);
        ProductMsg msg = new ProductMsg();
        msg.setProductId(productId);
        //默认都是待处理
        msg.setState(0);
        msg.setPrice(product.getSaleprice());
        msg.setTitle("购买：" + product.getName());
        msg.setResources(product.getResources());
        //获取当前购买人
        UserInfo userInfo = LoginContext.getUserInfo();
        User user = userMapper.findByLogininfoId(userInfo.getLogininfo().getId());
        msg.setUserId(user.getId());
        super.add(msg);
    }

    /**
     * 接收订单
     * @param dto
     */
    @Override
    public void accept(AcceptDto dto) {
        ProductMsg msg = findById(dto.getMsgId());
        //2.生成收购单
        OrderProduct order = new OrderProduct();
        order.setOrderSn(CodeGenerateUtils.generateOrderSn(msg.getUserId()));
        order.setDigest("xxxx接了单");
        order.setLastConfirmTime(new Date(System.currentTimeMillis() + 2*24*60*60*1000));
        order.setState(0);
        order.setPrice(msg.getPrice());
        order.setPaySn(UUID.randomUUID().toString());
        order.setProductId(msg.getProductId());
        order.setUserId(msg.getUserId());
        order.setMsgId(msg.getId());
        orderMapper.add(order);
        // 3.msg的状态改成已接单
        ProductMsg m = new ProductMsg();
        m.setId(msg.getId());
        m.setState(1);
        productMsgMapper.update(m);
    }

    //拒接订单
    @Override
    public void reject(Long id) {
        ProductMsg msg = new ProductMsg();
        msg.setId(id);
        msg.setState(-1);
        productMsgMapper.update(msg);
        //todo 添加日志，给该店铺减分，减少寻主信息的派送
    }
}
